<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配置名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入配置名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:fileConfig:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileConfigList">
      <el-table-column label="配置ID" align="center" prop="fileConfigId" width="100" />
      <el-table-column label="配置名称" align="center" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="配置值" align="center" prop="value" width="150" :show-overflow-tooltip="true" />
      <el-table-column label="区域" align="center" prop="region" width="120" />
      <el-table-column label="存储桶" align="center" prop="bucket" width="150" />
      <el-table-column label="端点" align="center" prop="endpoint" :show-overflow-tooltip="true" />
      <el-table-column label="CNAME" align="center" prop="cname" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.cname === '0'" type="danger">否</el-tag>
          <el-tag v-else type="success">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:fileConfig:edit']"
          >修改</el-button>
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:fileConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="fileConfigRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="配置名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="配置值" prop="value">
          <el-input v-model="form.value" placeholder="请输入配置值" />
        </el-form-item>
        <el-form-item label="区域" prop="region">
          <el-input v-model="form.region" placeholder="如: oss-cn-hangzhou" />
        </el-form-item>
        <el-form-item label="存储桶" prop="bucket">
          <el-input v-model="form.bucket" placeholder="请输入存储桶名称" />
        </el-form-item>
        <el-form-item label="Access Key ID" prop="accessKeyId">
          <el-input v-model="form.accessKeyId" placeholder="请输入Access Key ID" />
        </el-form-item>
        <el-form-item label="Access Key Secret" prop="accessKeySecret">
          <el-input v-model="form.accessKeySecret" type="password" placeholder="请输入Access Key Secret" show-password />
        </el-form-item>
        <el-form-item label="使用CNAME" prop="cname">
          <el-radio-group v-model="form.cname">
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="端点" prop="endpoint">
          <el-input v-model="form.endpoint" placeholder="请输入端点地址" />
        </el-form-item>
        <el-form-item label="超时时间(ms)" prop="timeout">
          <el-input-number v-model="form.timeout" controls-position="right" :min="0" :max="600000" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="FileConfig">
import { listFileConfig, getFileConfig, delFileConfig, addFileConfig, updateFileConfig } from "@/api/basic/fileConfig";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();
const { parseTime } = proxy;

const fileConfigList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null
  },
  rules: {
    name: [
      { required: true, message: "配置名称不能为空", trigger: "blur" }
    ],
    value: [
      { required: true, message: "配置值不能为空", trigger: "blur" }
    ],
    region: [
      { required: true, message: "区域不能为空", trigger: "blur" }
    ],
    bucket: [
      { required: true, message: "存储桶不能为空", trigger: "blur" }
    ],
    accessKeyId: [
      { required: true, message: "Access Key ID不能为空", trigger: "blur" }
    ],
    accessKeySecret: [
      { required: true, message: "Access Key Secret不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文件配置列表 */
function getList() {
  loading.value = true;
  listFileConfig(queryParams.value).then(response => {
    fileConfigList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    fileConfigId: null,
    name: null,
    value: null,
    region: null,
    bucket: null,
    accessKeyId: null,
    accessKeySecret: null,
    cname: "0",
    endpoint: null,
    timeout: 300000,
    remark: null
  };
  proxy.resetForm("fileConfigRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加文件配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getFileConfig(row.fileConfigId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改文件配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fileConfigRef"].validate(valid => {
    if (valid) {
      if (form.value.fileConfigId != null) {
        updateFileConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFileConfig(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除文件配置"' + row.name + '"？').then(function() {
    return delFileConfig(row.fileConfigId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
